﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Main_Accounts_VoucherFormat : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["action"] != null)
        {
            if (Request.QueryString["action"].ToString() == "addnew")
            {

                if (!IsPostBack)
                {
                    PnlAddNew.Visible = true;
                    string[] datearr = Session["booksfromdate"].ToString().Split('/');
                    txtEffectiveDate.Text = datearr[1].ToString() + "/" + datearr[0].ToString() + "/" + datearr[2].ToString();
                    BindParentVoucher();

                }
            }
            else if (Request.QueryString["action"].ToString() == "view")
            {
                if (!IsPostBack)
                {
                    PnlView.Visible = true;
                    BindGrid();
                }
            }
        }

    }
    protected void BindParentVoucher()
    {
        strsql = "SELECT TYPEID,PARENTTYPEID,TRANSACTIONNAME FROM ACC_TRANSETTINGS WHERE TYPEID=PARENTTYPEID AND TYPEID<>0";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            ddlParentVoucher.DataSource = DT;
            ddlParentVoucher.DataTextField = "TRANSACTIONNAME";
            ddlParentVoucher.DataValueField = "TYPEID";
            ddlParentVoucher.DataBind();
            ddlParentVoucher.Items.Insert(0, new ListItem("-Select Parent-", "-1"));
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        InsertTransactionMain();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {       
        Response.Redirect("~/Welcome.aspx");
    }
    protected bool Validate()
    {
        bool flag = true;
        strsql = "select TRANSACTIONNAME FROM ACC_TRANSETTINGS WHERE TRANSACTIONNAME='"+txtVoucherName.Text.Trim()+"'";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            flag = false;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Specified Voucher Name Alredy Exist');", true);
           
        }
        strsql = "select ABBREVIATION FROM ACC_TRANSETTINGS WHERE ABBREVIATION='" + txtAbbreviation.Text.Trim() + "' and ABBREVIATION<>''";
        DataTable DT1 = DU.getdata(strsql);
        if (DT1.Rows.Count > 0)
        {
            flag = false;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Specified ABBREVIATION Name Alredy Exist');", true);
            
        }
        return flag;
    }
    protected void InsertTransactionMain()
    {
        if (Validate())
        {
            string effectiveDate = string.Empty;
            string abbreviation = string.Empty;
            string billformatid = "0";
            if (txtEffectiveDate.Text.ToString() != string.Empty)
            {
                string[] date1 = txtEffectiveDate.Text.ToString().Split('/');
                effectiveDate = date1[1].ToString() + "/" + date1[0].ToString() + "/" + date1[2].ToString();
            }
            if (txtAbbreviation.Text.ToString() != string.Empty)
            {
                abbreviation = txtAbbreviation.Text.Trim();
            }
            else
            {
                abbreviation = txtVoucherName.Text.Trim();
            }
            if (radioStandard.SelectedValue == "0")
            {
                billformatid = "0";
            }
            if (radioStandard.SelectedValue == "2")
            {
                billformatid = "2";
            }

            SqlParameter[] sparam =
                                        {
                                            new SqlParameter("@TYPEID","-1"),
                                            new SqlParameter("@PARENTTYPEID",ddlParentVoucher.SelectedValue),
                                            new SqlParameter("@TRANSACTIONNAME",txtVoucherName.Text.Trim()),
                                            new SqlParameter("@ISAUTONO",chkAutoNumber.Checked),
                                            new SqlParameter("@RESETNO",chkNewNumber.Checked),
                                            new SqlParameter("@ISREFERENCE",chkApplyReference.Checked),
                                            new SqlParameter("@ABBREVIATION",abbreviation),
                                            new SqlParameter("@ISDEFAULTNARRATION","0"),
                                            new SqlParameter("@EFFECTIVEDATE",effectiveDate),
                                            new SqlParameter("@BILLFORMATID",billformatid),
                                            new SqlParameter("@MODULETYPE","1"),
                                            new SqlParameter("@CCALLOCATIONTYPE","0")
                                        };
            int result = DU.executeprocedure("USP_ACC_SaveTransactionSetting", sparam);
            if (result > 0)
            {
                strsql = "Select Ident_Current('ACC_TRANSETTINGS')as TYPEID";
                DataTable DT = DU.getdata(strsql);
                if (DT.Rows.Count > 0)
                {
                    InsertTransactionDetails(DT.Rows[0]["TYPEID"].ToString());
                }
            }            
        }
    }
    protected void InsertTransactionDetails(string typeid)
    {
        if (typeid != string.Empty)
        {
            if (chkAutoNumber.Checked != true)
            {
                txtStartNumber.Text = "0";
            }
            SqlParameter[] sparam =
                                        {
                                            new SqlParameter("@TYPEID",typeid),
                                            new SqlParameter("@FINYEAR",Session["FINYEAR"].ToString()),
                                            new SqlParameter("@STARTNO",txtStartNumber.Text.Trim()),
                                            new SqlParameter("@PREFIX",txtPrefix.Text.Trim()),                                            
                                            new SqlParameter("@SUFFIX",txtSuffix.Text.Trim()),
                                            new SqlParameter("@ACCLEVELNARRATION","0"),
                                            new SqlParameter("@NARRATION",txtCommonNarration.Text.Trim()),
                                            new SqlParameter("@PRINTONSAVE",chkPrintAfterSave.Checked)
                                            
                                        };
           int result= DU.executeprocedure("USP_ACC_SaveTransactionSettingDetail", sparam);
           if (result > 0)
           {
               ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Inserted');", true);
           }
        }        
    }
    // For View 
    protected void BindGrid()
    {
        strsql = "select TYPEID, transactionname, Effectivedate  from acc_transettings";
        DataTable dt =DU.getdata(strsql);
        GrdVoucher.DataSource = dt;
        GrdVoucher.DataBind();
    }

    protected void GrdVoucher_SelectedIndexChanged(object sender, EventArgs e)
    {
        string TYPEID = GrdVoucher.SelectedDataKey.Value.ToString();
        Response.Redirect("VoucherFormatEdit.aspx?typeid=" + TYPEID);
    }
}
                                